package com.samsung.service.impl.vote;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.samsung.common.util.BaseUtil;
import com.samsung.dao.vote.OptionMapper;
import com.samsung.dao.vote.TopicMapper;
import com.samsung.dao.vote.VoteDataMapper;
import com.samsung.dao.vote.VoteHistoryMapper;
import com.samsung.dto.vote.CreateVoteDto;
import com.samsung.model.vote.VoteOption;
import com.samsung.model.vote.VoteTopic;
import com.samsung.service.vote.UpdateVoteService;

@Service("UpdateVoteService")
public class UpdateVoteServiceImpl implements UpdateVoteService {

	@Resource
	private VoteDataMapper voteDataMapper;

	@Resource
	private TopicMapper topicMapper;

	@Resource
	private OptionMapper optionMapper;

	@Resource
	private VoteHistoryMapper voteHistoryMapper;

	@Override
	public boolean checkVoteData(String tid) {
		 List<?> resultList=null;
		 boolean flag=false;
		 resultList=voteDataMapper.getVoteHistory(tid);
		 if(resultList!=null&&resultList.size()>=1){
			 flag=false;
		 }else{
			 flag=true;
		 }	
		return flag;
	}



	@Override
	public Map<String, Object> deleteVoteRecord(String tid) {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//删除投票标题对应的选项内容
			voteDataMapper.deleteVoteOptoionById(tid);
			//删除投票标题对应内容
			voteDataMapper.deleteVoteTpById(tid);
		} catch (Exception e) {
			returnMap.put("code", "failure");
			returnMap.put("message", "投票话题删除成失败！");
			e.printStackTrace();
		}
		return returnMap;
	}



	@Override
	public VoteTopic selectTopic(String tid) {
		VoteTopic topic =topicMapper.selectByPrimaryKey(tid);
		return topic;
	}



	@Override
	public CreateVoteDto selectOption(String tid) {
		List<VoteOption> optionList = new ArrayList<VoteOption>();
		optionList = optionMapper.listByTopic(tid);
		CreateVoteDto createVoteDto = new CreateVoteDto();
		int i = 0;
		for (VoteOption option :optionList) {
			i++;
			if (i==1) {
				createVoteDto.setContent1(option.getContent());	
			}
			if (i==2) {
				createVoteDto.setContent2(option.getContent());	
			}
			if (i==3) {
				createVoteDto.setContent3(option.getContent());	
			}
			if (i==4) {
				createVoteDto.setContent4(option.getContent());	
			}
			if (i==5) {
				createVoteDto.setContent5(option.getContent());	
			}
			if (i==6) {
				createVoteDto.setContent6(option.getContent());	
			}
			if (i==7) {
				createVoteDto.setContent7(option.getContent());	
			}
			if (i==8) {
				createVoteDto.setContent8(option.getContent());	
			}
			if (i==9) {
				createVoteDto.setContent9(option.getContent());	
			}
			if (i==10) {
				createVoteDto.setContent10(option.getContent());	
			}
		}
		return createVoteDto;
	}



	@Override
	public int countContent(CreateVoteDto createVoteDto) {
		int count = 0;
		if (createVoteDto.getContent1() != null) {
			++count;
		}
		if (createVoteDto.getContent2() != null) {
			++count;
		}
		if (createVoteDto.getContent3() != null) {
			++count;
		}
		if (createVoteDto.getContent4() != null) {
			++count;
		}
		if (createVoteDto.getContent5() != null) {
			++count;
		}
		if (createVoteDto.getContent6() != null) {
			++count;
		}
		if (createVoteDto.getContent7() != null) {
			++count;
		}
		if (createVoteDto.getContent8() != null) {
			++count;
		}
		if (createVoteDto.getContent9() != null) {
			++count;
		}
		if (createVoteDto.getContent10() != null) {
			++count;
		}
		return count;
	}



	@Override
	public Map<String, Object> checkTitle(String title, String compareName) {
		Map<String,Object> model = new HashMap<String,Object>();
		if (BaseUtil.isEmpty(title.trim())) {
			model.put("message", "请输入标题");
			model.put("flag", "failure");
			return model;
		}
		Map<String, String> data = new HashMap<String, String>();
		data.put("title", title);
		data.put("compareName", compareName);
		if (topicMapper.checkUpdateTitle(data).size() > 0) {
			model.put("message", "标题已经存在，请重新命名");
			model.put("flag", "failure");
			return model;
		} 
		model.put("flag", "success");
		return model;
	}



	@Override
	public boolean checkTitleData(String tid, String uid) {
		 boolean flag=false;
		 topicMapper.selectByPrimaryKey(tid);
		 if(topicMapper.selectByPrimaryKey(tid).getUid().equals(uid)){
			 flag=false;
		 }else{
			 flag=true;
		 }	
		return flag;
	}


/**
 * 新增开始
 */
	@Override
	public List<Map<String, Object>> getVoteDetail(String tid) {
		return voteHistoryMapper.getVoteDetail(tid);
	}



	@Override
	public Map<String, String> getVoteCount(String tid) {
		Map<String, String> model = new HashMap<String, String>();
		int count = voteHistoryMapper.getVoterCountByTopic(tid);
		if( count>0 ){
			model.put("message", "已经有用户投票了，不能修改了哦");
			model.put("flag", "failure");
		}else{
			model.put("flag", "success");
		}
		return model;
	}
	
/**
 * 新增结束
 */
}